package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    /**
     * 查询所有频道
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    @Override
    public ResponseResult listAll(WmChannelDto dto) {
        //1.检查参数
        dto.checkParam();

        //2.设置查询条件
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(WmChannel::getName,dto.getName());
        queryWrapper.orderByDesc(WmChannel::getOrd);

        Page page = new Page(dto.getPage(), dto.getSize());
        page(page,queryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult deleteById(int id) {

        WmChannel wmChannel = getOne(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getId, id).eq(WmChannel::getStatus, 1));
        if (wmChannel != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_DELETE_FAILURE);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmChannel::getName,wmChannel.getName());
        WmChannel wmChannel1 = getOne(lambdaQueryWrapper);
        if (wmChannel1 != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmChannel.setCreatedTime(new Date());
        save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private WmNewsService wmNewsService;
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        WmChannel wmChannel1 = getById(wmChannel.getId());
        if (wmChannel1 == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (wmChannel1.getStatus() && !wmChannel.getStatus()) {
            LambdaQueryWrapper<WmNews> queryWrapper = new LambdaQueryWrapper<>();
            List<WmNews> list = wmNewsService.list(queryWrapper.eq(WmNews::getChannelId, wmChannel.getId()));
            if (list.size() > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_DISABLED_FAILURE);
            }
        }
        BeanUtils.copyProperties(wmChannel,wmChannel1);
        updateById(wmChannel1);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
